<?php
/**
 * 身份证审核
 * User: ws
 * Date: 2018/11/2
 * Time: 10:28
 */

namespace app\admin\controller;
use think\Db;
class Userid    extends Init
{
    function index(){
        if ($this->request->isAjax()) {
            $key = input('key')?input('key'):'';
            $sj = input('sj')?input('sj'):'';
            if(is_numeric(input('state')) === true){
                $where['us.state'] = input('state');
            }
            if(!empty($sj)){
                $sj = explode(' - ',$sj);
                $where['us.create_time'] = array(array('egt',strtotime($sj[0])),array('elt',strtotime($sj[1]))) ;
            }
            $where['us.name|us.number|u.name'] = array('like','%'.trim($key).'%');
            $list = Db::table('je_userid')->alias('us')->join('je_user u','us.user_id = u.user_id')->field('u.name as uname,u.img as uimg,us.*')->order('id desc')->where($where)->paginate(input('limit'))->toArray();
            foreach ($list['data'] as $key=>$itme){
                $list['data'][$key]['img'] = explode(',',$itme['img']);
            }
            return json($result = ['code'=>0,'msg'=>'获取成功','data'=>$list['data'],'count'=>$list['total']]);
        }
        return view('list');
    }

    function edit(){
        if(request()->isPost()){
            $data = $this->request->post();
            $data['admin_user_id'] =  $_SESSION['think']['admin_user']['admin_user_id'];
            $data['audit_time'] = time();
            if($data['state']==2){
                if(empty($data['remarks'])){
                    return json(array('code'=>0,'msg'=>'操作失败，请填写不通过理由'));
                }
            }
            $result = db('userid')->update($data);
            if($result){
                return json(array('code'=>200,'msg'=>'审核成功'));
            }else{
                return json(array('code'=>0,'msg'=>'审核失败'));
            }
        }


        $id = input('id');
        if(empty($id)){
            return json($result = ['code'=>0,'msg'=>'缺少参数']);
        }

        $info = Db::table('je_userid')->alias('us')->join('je_user u','us.user_id = u.user_id')->field('u.name as uname,u.img as uimg,u.time,u.phone,us.*')->order('id desc')->where("us.id={$id}")->find();
        $info['img'] = explode(',',$info['img']);

        if(!empty($info['admin_user_id'])){$info['admin_user_id'] = Db::name('admin_user')->where("admin_user_id={$info['admin_user_id']}")->value('name');}

        return view('edit',['list'=>$info]);
    }
}